package com.yswb.webSocket;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

public class Server {
	private static final int PORT = 7878;
	
	protected static final int BIZTHREADSIZE = 1;
	
	private static EventLoopGroup bossGroup=new NioEventLoopGroup(BIZTHREADSIZE);
	private static EventLoopGroup workerGroup=new NioEventLoopGroup(BIZTHREADSIZE);
	public static void run(){
		try {
			ServerBootstrap b=new ServerBootstrap();
			b.group(bossGroup, workerGroup);
			b.channel(NioServerSocketChannel.class);
			b.childHandler(new WebSocketServerInitializer());
			
			b.option(ChannelOption.SO_BACKLOG, 65536)           
            .childOption(ChannelOption.SO_KEEPALIVE, true)  
            .childOption(ChannelOption.TCP_NODELAY, true);
			// 服务器绑定端口监听
        	ChannelFuture f = b.bind(PORT).sync();
            // 监听服务器关闭监听
			f.channel().closeFuture().sync();
		} catch (InterruptedException e) {
			System.out.println("端口被占用:"+PORT);
			//e.printStackTrace();
		}
		finally{
			bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
		}
	}
	
	public static void main(String[] args) {
		run();
	}
}
